package hu.plusone.fakecommander.util;

import com.flickr4java.flickr.Flickr;
import com.flickr4java.flickr.FlickrException;
import com.flickr4java.flickr.REST;
import com.flickr4java.flickr.auth.Auth;
import com.flickr4java.flickr.auth.AuthInterface;
import com.flickr4java.flickr.auth.Permission;
import hu.plusone.fakecommander.flickr.FlickrProperties;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Random;
import java.util.Scanner;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.scribe.model.Token;
import org.scribe.model.Verifier;

public class Utils {

    private static final Random RANDOM = new Random(System.currentTimeMillis());
    private static final int COLOR_MAX = 256 * 256 * 256;

    private static Color randomColor() {
        return new Color(RANDOM.nextInt(COLOR_MAX));
    }

    private Utils() {
    }

    public static void findToken() throws FlickrException, IOException {
        Properties properties = new Properties();
        properties.load(ClassLoader.getSystemResourceAsStream("settings.properties"));

        String apiKey = FlickrProperties.API_KEY.getProperty();
        String apiSecret = FlickrProperties.API_SECRET.getProperty();
        System.out.println("key: " + apiKey);
        System.out.println("sec: " + apiSecret);
        apiKey = properties.getProperty(apiKey);
        apiSecret = properties.getProperty(apiSecret);
        System.out.println("key: " + apiKey);
        System.out.println("sec: " + apiSecret);

        Flickr flickr = new Flickr(apiKey, apiSecret, new REST());

        AuthInterface authInterface = flickr.getAuthInterface();

        Scanner scanner = new Scanner(System.in);

        Token token = authInterface.getRequestToken();
        System.out.println("token: " + token);

        String url = authInterface.getAuthorizationUrl(token, Permission.DELETE);
        System.out.println("Follow this URL to authorise yourself on Flickr");
        System.out.println(url);
        System.out.println("Paste in the token it gives you:");
        System.out.print(">>");

        String tokenKey = scanner.nextLine();

        Token requestToken = authInterface.getAccessToken(token, new Verifier(tokenKey));
        System.out.println("Authentication success");

        Auth auth = authInterface.checkToken(requestToken);

        // This token can be used until the user revokes it.
        System.out.println("Token: " + requestToken.getToken());
        System.out.println("Token sec: " + requestToken.getSecret());
        System.out.println("nsid: " + auth.getUser().getId());
        System.out.println("Realname: " + auth.getUser().getRealName());
        System.out.println("Username: " + auth.getUser().getUsername());
        System.out.println("Permission: " + auth.getPermission().getType());
    }

    public static String encodeBase64(String text) {
        return new String(Base64.encodeBase64(text.getBytes()));
    }

    public static String decodeBase64(String text) {
        return new String(Base64.decodeBase64(text.getBytes()));
    }

    public static BufferedImage generateRandomPicture() {
        BufferedImage img = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < img.getWidth(); i++) {
            for (int j = 0; j < img.getHeight(); j++) {
                Color color = randomColor();
                img.setRGB(i, j, color.getRGB());
            }
        }
        return img;
    }

    public static byte[] generateRandomPictureData() {
        try {
            BufferedImage pic = generateRandomPicture();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(pic, "jpg", baos);
            baos.flush();
            byte[] imageInByte = baos.toByteArray();
            baos.close();
            return imageInByte;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
